﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DLM.Infrastructure.Email;
using System.Configuration;
using System.IO;
using DLM.Domain.Model;

namespace DLM.Services.Implementations
{
    public class MailService
    {
        private string templatePath;
        private string invitationTemplatePath;
        private string invitationContent;
        private string resetTemplatePath;
        private string resetContent;
        private string content;
        private string url;
        private string partyUrl;
        private string resetUrl;
        private IEmailService mailService;

        public MailService(IEmailService mailService)
        {
            this.mailService = mailService;
            string serverPath = System.Web.HttpContext.Current.Server.MapPath("~/");
            this.templatePath = serverPath  + ConfigurationManager.AppSettings["TemplatePath"];
            this.content = File.ReadAllText(this.templatePath, Encoding.UTF8);
            this.invitationTemplatePath = serverPath + ConfigurationManager.AppSettings["InvitationTemplatePath"];
            this.invitationContent = File.ReadAllText(this.invitationTemplatePath,Encoding.UTF8);
            this.resetTemplatePath = serverPath + ConfigurationManager.AppSettings["ResetPath"];
            this.resetContent = File.ReadAllText(this.resetTemplatePath, Encoding.UTF8);
            this.url = ConfigurationManager.AppSettings["ActiveUrl"];
            this.partyUrl = ConfigurationManager.AppSettings["PartyUrl"];
            this.resetUrl = ConfigurationManager.AppSettings["ResetUrl"];

        }


        public void SendRegisterNotice(Account account)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(this.content);
            sb.Replace(@"[Account]", account.AccountName);
            sb.Replace(@"[Code]", account.ActiveCode);
            sb.Replace(@"[Url]", this.url + account.ActiveCode);
            this.mailService.SendEmail("", account.Email, "都来米欢迎您", sb.ToString());
        }

        public void SendRetrievePasswordNotice(Account account)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(this.resetContent);
            sb.Replace(@"[Account]", account.AccountName);
            sb.Replace(@"[Url]", this.resetUrl + account.ActiveCode);
            this.mailService.SendEmail("", account.Email, "都来米欢-密码找回", sb.ToString());
        }

        public void SendInvitationNotice(EmailInvitation invitation)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(this.invitationContent);

            sb.Replace(@"[Account]", invitation.AccountName);
            sb.Replace(@"[Friend]",invitation.Content);
            sb.Replace(@"[Subject]", invitation.PartySubject);
            sb.Replace(@"[URL]", this.partyUrl + invitation.PartyID);
            this.mailService.SendEmail("",invitation.Email,"都来米-活动邀请",sb.ToString());
        }

    }
}
